package neusoft.yuejian_11.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import neusoft.yuejian_11.base.BaseActivity;
import neusoft.yuejian_11.bean.AttentionBean;
import neusoft.yuejian_11.bean.IsAttentionBean;
import neusoft.yuejian_11.bean.MasterBean;
import neusoft.yuejian_11.listener.RetrofitListener;
import neusoft.yuejian_11.model.AttentionModel;
import neusoft.yuejian_11.model.MasterModel;
import neusoft.yuejian_11.R;

public class MasterDetailActivity extends BaseActivity implements RetrofitListener {
    protected Context context;
    private int masterId;
    private String masterName;
    private String masterPic;
    private TextView textName;
    private ImageButton imageButtonBack;
    private TextView masterInfoView;
    private ImageView imageView;
    private  ImageButton favorButton;
    private  MasterBean masterBean;
    private Button buttonCourse;
    private Button buttonMvedio;
    private Boolean flags;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_master_detail);

        initView();
        initEvents();
        initData();


    }

    private void initData() {
        masterId = getIntent().getIntExtra("pm_id",-1);
        masterName = getIntent().getStringExtra("pm_name");
        textName.setText(masterName);
        masterPic = getIntent().getStringExtra("pm_pic");
        Picasso.with(context)
                .load(masterPic)
                .into(imageView);

        AttentionModel attentionModel=new AttentionModel();
        attentionModel.isCollectedM(String.valueOf(getU_id()),String.valueOf(masterId), "3",this);

        MasterModel masterModel = new MasterModel();
        masterModel.getMasterById(String.valueOf(masterId),this);
    }

    private void initView() {
        textName = (TextView) findViewById(R.id.textView_master_name);
        imageView = (ImageView) findViewById(R.id.imageView);
        imageButtonBack = (ImageButton) findViewById(R.id.imageButtom_back);
        buttonCourse = (Button) findViewById(R.id.button_mcourse);
        masterInfoView = (TextView) findViewById(R.id.text_master_introduct);
        imageView= (ImageView) findViewById(R.id.imageView);
        favorButton= (ImageButton) findViewById(R.id.master_collect);
        buttonCourse = (Button) findViewById(R.id.button_mcourse);
        buttonMvedio= (Button) findViewById(R.id.button_mvideo);
        imageButtonBack = (ImageButton) findViewById(R.id.imageButtom_back);
    }

    private void initEvents() {
        //用户收藏大师
        favorButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AttentionModel attentionModel = new AttentionModel();
                attentionModel.userCollectMaster(String.valueOf(getU_id()), String.valueOf(masterId), MasterDetailActivity.this);
            }
        });
        buttonCourse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MasterDetailActivity.this, McourseListActivity.class);
                intent.putExtra("pm_id",masterId);
                intent.putExtra("pm_name",masterName);
                intent.putExtra("pm_pic",masterPic);
                startActivity(intent);
            }
        });
        buttonMvedio.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MasterDetailActivity.this, MasterVideoActivity.class);
                intent.putExtra("pm_id",masterId);
                intent.putExtra("pm_name",masterName);
                startActivity(intent);
            }
        });
        imageButtonBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }


    @Override
    public void onSuccess(Object o, int flag) {
        if (flag == 1) {
            //大师详情
            MasterBean masterBean = (MasterBean) o;
            masterInfoView.setText(masterBean.getPm_introduct());
        }
        //判断某条晒图是否已经收藏
        if (flag == 3) {
                IsAttentionBean isAttentionBean = (IsAttentionBean) o;
                if (isAttentionBean.getCollected().equals("1")) {
                    //已关注
                    flags = true;
                    favorButton.setImageResource(R.drawable.ic_alikepost);
                } else {
                    //未关注的
                    flags = false;
                    favorButton.setImageResource(R.drawable.ic_likepost);
                }

        }
        //用户收藏大师
        if (flag == 4) {
            AttentionBean attentionBean = (AttentionBean) o;
            if (attentionBean.getSuccess().equals("1")) {
                Log.e(attentionBean.getSuccess(), "---------");
                if (flags) {
                    flags = false;
                    favorButton.setImageResource(R.drawable.ic_likepost);
                } else {
                    flags = true;
                    favorButton.setImageResource(R.drawable.ic_alikepost);

                }
                Toast.makeText(MasterDetailActivity.this, "操作成功", Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    public void onFail() {

    }

}
